import { _decorator, Component, Label, Node, Sprite, SpriteFrame, Vec3 } from 'cc';
import { losChannel } from 'db://assets/app/Script/SDK/losChannel';
import { StaminaSystem } from 'db://assets/app/Script/StaminaSystem/StaminaSystem';
import { losTools } from 'db://assets/app/Script/Tools/losTools';
import { _ui } from 'db://assets/main/script/Main';

const { ccclass, property } = _decorator;


@ccclass('NewRewardBoxFrame')
export default class newRewardBoxFrame extends Component {
	@property({ type: [Node], tooltip: '全部宝箱节点' })
	allBoxNode: Node[] = [];
	@property(Node)
	node_close: Node | null = null;
	/**获得奖励 */
	@property(Node)
	node_getReward: Node | null = null;
	/**获得全部奖励 */
	@property(Node)
	node_getAll: Node | null = null;
	/**奖励图标(0:体力/1:交换 2:冰冻 3:提示) */
	@property([SpriteFrame])
	iconSprArr: SpriteFrame[] = [];







	//已经开过的箱子下标
	hasOpenBoxIdxArr: number[] = [];
	freeOpenCnt: number = 0;

	protected onLoad(): void {
		losChannel.instance().doAction(22, ['hw'], (enabled, val) => {
			if (enabled) {
				this.node_getReward.getChildByName('videoSp').active = false;
				this.node_getReward.getChildByName('videoLb').active = true;

				this.node_getAll.getChildByName('videoSp').active = false;
				this.node_getAll.getChildByName('videoLb').active = true;
			}
			else {
				this.node_getReward.getChildByName('videoSp').active = true;
				this.node_getReward.getChildByName('videoLb').active = false;

				this.node_getAll.getChildByName('videoSp').active = true;
				this.node_getAll.getChildByName('videoLb').active = false;
			}
		});


		this.freeOpenCnt = losTools.getRandomInt(1, 3);
		this.node_close.on(Node.EventType.TOUCH_END, () => {
			this.node.destroy();
		}, this);
		this.node_getReward.on(Node.EventType.TOUCH_END, () => {
			losChannel.instance().showVideo({
				sucCall: () => {
					let upPower = losTools.getRandomInt(1, 3);
					StaminaSystem.instance.addStamina(upPower);
					_ui.tip('体力增加' + upPower)
				},
				failCall: () => {

				},
				endCall: () => {

				}
			});
		}, this);

		this.node_getAll.on(Node.EventType.TOUCH_END, () => {
			losChannel.instance().showVideo({
				sucCall: () => {
					let upPower = 1;
					const chName = losChannel.instance().channel;
					switch (chName) {
						case 'ks':
							upPower = losTools.getRandomInt(5, 10);
							break;
						case 'ks2':
						case 'ks3':
							upPower = 3;
							break
						default:
							upPower = 3;
							break;
					}
					StaminaSystem.instance.addStamina(upPower);
					_ui.tip('体力增加' + upPower)
				},
				failCall: () => {
				},
				endCall: () => {
				}
			});

		}, this);
		for (let i = 0; i < this.allBoxNode.length; i++) {
			this.allBoxNode[i].getChildByName('video').active = false;
			this.allBoxNode[i].getChildByName('videoLb').active = false;
		}
		for (let i = 0; i < this.allBoxNode.length; i++) {
			this.allBoxNode[i].on(Node.EventType.TOUCH_END, () => {
				if (this.hasOpenBoxIdxArr.indexOf(i) >= 0) {
					console.error("箱子已经被打开过");
					return;
				}
				if (this.freeOpenCnt > 0) {
					this.freeOpenCnt--;
					this.getOpenBoxReward(i);
					if (this.freeOpenCnt == 0) {
						for (let i = 0; i < this.allBoxNode.length; i++) {
							if (this.hasOpenBoxIdxArr.indexOf(i) >= 0) {
								continue;
							}
							this.allBoxNode[i].getChildByName('video').active = true;
							losChannel.instance().doAction(22, ['hw'], (enabled, val) => {
								if (enabled) {
									this.allBoxNode[i].getChildByName('videoLb').active = true;
									this.allBoxNode[i].getChildByName('video').active = false;
								}
								else {
									this.allBoxNode[i].getChildByName('videoLb').active = false;
									this.allBoxNode[i].getChildByName('video').active = true;
								}
							});
						}
					}
				} else {
					let me = this;
					losChannel.instance().showVideo({
						sucCall: () => {
							/**看视频打开箱子 */
							console.error("看视频打开箱子");
							me.getOpenBoxReward(i);
						},
						failCall: () => {

						},
						endCall: () => {

						}
					});
				}
			});
		}
	}
	/**获取宝箱里的奖励 
	 * idx:  宝箱下标
	*/
	getOpenBoxReward(idx: number) {
		this.hasOpenBoxIdxArr.push(idx);
		this.allBoxNode[idx].getChildByName('rewardBox1').active = false;
		this.allBoxNode[idx].getChildByName('video').active = false;
		this.allBoxNode[idx].getChildByName('videoLb').active = false;
		let power = 1;
		this.allBoxNode[idx].getChildByName('reward').scale = new Vec3(0.8, 0.8);
		this.allBoxNode[idx].getChildByName('reward').getComponent(Sprite).spriteFrame = this.iconSprArr[0];
		this.allBoxNode[idx].getChildByName('rewardNum').getComponent(Label).string = power + '';
		console.error("获得的体力: ", power);
		/**增加体力 */
		StaminaSystem.instance.addStamina(power);
	}
}

